﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Card.Manager
{
    public partial class SiteSetting : BasePage
    {
        public string SiteCode
        {
            get
            {
                if (Request.QueryString["c"] != null)
                {
                    return Request.QueryString["c"];
                }
                return string.Empty;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.rptCards.ItemCommand += new RepeaterCommandEventHandler(rptCards_ItemCommand);
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            base.OnInit(e);
        }

        void rptCards_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName, "SAVE") == 0 || string.Compare(e.CommandName, "ADD") == 0)
            {
                Entity.CardSetting setting = new Card.Entity.CardSetting();
                List<Entity.CardPriceSetting> lstPriceSetting = new List<Card.Entity.CardPriceSetting>();
                if (CheckSaveCardSetting(e.Item, setting, lstPriceSetting))
                {
                    BLL.CardSettingManager.Save(setting, this.CPassport);
                    foreach (Entity.CardPriceSetting ps in lstPriceSetting)
                    {
                        BLL.CardPriceSettingManager.Save(ps, this.CPassport);
                    }
                    this.MessageBox("提交成功！");
                }
                
            }
            else if (string.Compare(e.CommandName, "DELETE") == 0)
            {
                string code = e.CommandArgument.ToString();
                BLL.CardSettingManager.Delete(code, this.SiteCode, this.CPassport);
                BLL.CardSettingManager.DeleteSetting(code, this.SiteCode, this.CPassport);
                this.MessageBox("删除成功！");
            }
            this.DoInitCardSetting();
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName, "SAVE") == 0)
            {
                foreach (RepeaterItem item in this.rptList.Items)
                {
                    if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
                    {
                        string strSettingType = ((Label)item.FindControl("lblCode")).Text;
                        string strVal = ((TextBox)item.FindControl("txtTextValue")).Text;
                        Entity.SiteSetting setting = BLL.SiteSettingManager.GetEntity(strSettingType, this.SiteCode, this.CPassport);
                        if (setting == null)
                        {
                            setting = new Card.Entity.SiteSetting();
                            setting.CreatedDate = DateTime.Now;
                            setting.CreatedBy = this.Passport.UserID;
                        }
                        setting.SettingType = strSettingType;
                        setting.SiteCode = this.SiteCode;
                        setting.Value = strVal;
                        BLL.SiteSettingManager.Save(setting, this.CPassport);
                    }
                }
                this.MessageBox("保存成功！");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        }

        private void DoInit()
        {
            DataTable dt = BLL.SiteSettingManager.GetSittingBySite(this.SiteCode, this.CPassport);
            this.rptList.DataSource = dt;
            this.rptList.DataBind();

            this.DoInitCardSetting();
        }

        private void DoInitCardSetting()
        {
            DataTable dtSetting = BLL.CardSettingManager.GetSettingInfo(this.SiteCode, this.CPassport);
            if (dtSetting == null || dtSetting.Rows.Count <= 0)
            {
                copySetting.Visible = true;
            }
            else
            {
                copySetting.Visible = false;
            }

            this.rptCards.DataSource = dtSetting;
            this.rptCards.DataBind();
        }

        private bool CheckSaveCardSetting(RepeaterItem item, Entity.CardSetting setting, List<Entity.CardPriceSetting> lstPriceSetting)
        {
            if (item.ItemType == ListItemType.Footer)
            {
                string txtCode = string.Empty;
                if (!CheckTextBoxEmpty(item, "txtCode", "纸张代码不能为空!", false, out txtCode))
                {
                    return false;
                }
                setting.CardCode = txtCode;
            }
            else
            {
                Label lblCode = item.FindControl("lblCode") as Label;
                if (lblCode == null || string.IsNullOrEmpty(lblCode.Text))
                {
                    this.MessageBox("纸张代码不能为空!");
                    return false;
                }
                setting.CardCode = lblCode.Text;
            }

            string txtCardName = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtCardName", "纸张名称不能为空!", false, out txtCardName))
            {
                return false;
            }
            setting.CardName = txtCardName;

            string strDesc = string.Empty;
            CheckTextBoxEmpty(item, "txtDesc", string.Empty, false, out strDesc);
            setting.Decription = strDesc;


            string txtSort = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtSort", "单价不能为空且必须为数字!", true, out txtSort))
            {
                return false;
            }
            setting.Sort = int.Parse(txtSort);
            setting.Area = this.SiteCode;


            string txtPrice2 = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtPrice2", "单价不能为空且必须为数字!", true, out txtPrice2))
            {
                return false;
            }
            lstPriceSetting.Add(new Card.Entity.CardPriceSetting()
            {
                Area = this.SiteCode,
                CardCode = setting.CardCode,
                Num = 2,
                Price = decimal.Parse(txtPrice2)
            });


            string txtPrice5 = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtPrice5", "单价不能为空且必须为数字!", true, out txtPrice5))
            {
                return false;
            }
            lstPriceSetting.Add(new Card.Entity.CardPriceSetting()
            {
                Area = this.SiteCode,
                CardCode = setting.CardCode,
                Num = 5,
                Price = decimal.Parse(txtPrice5)
            });

            string txtPrice10 = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtPrice10", "单价不能为空且必须为数字!", true, out txtPrice10))
            {
                return false;
            }
            lstPriceSetting.Add(new Card.Entity.CardPriceSetting()
            {
                Area = this.SiteCode,
                CardCode = setting.CardCode,
                Num = 10,
                Price = decimal.Parse(txtPrice10)
            });

            string txtPrice20 = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtPrice20", "单价不能为空且必须为数字!", true, out txtPrice20))
            {
                return false;
            }
            lstPriceSetting.Add(new Card.Entity.CardPriceSetting()
            {
                Area = this.SiteCode,
                CardCode = setting.CardCode,
                Num = 20,
                Price = decimal.Parse(txtPrice20)
            });

            string txtPrice50 = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtPrice50", "单价不能为空且必须为数字!", true, out txtPrice50))
            {
                return false;
            }
            lstPriceSetting.Add(new Card.Entity.CardPriceSetting()
            {
                Area = this.SiteCode,
                CardCode = setting.CardCode,
                Num = 50,
                Price = decimal.Parse(txtPrice50)
            });

            string txtPrice100 = string.Empty;
            if (!CheckTextBoxEmpty(item, "txtPrice100", "单价不能为空且必须为数字!", true, out txtPrice100))
            {
                return false;
            }
            lstPriceSetting.Add(new Card.Entity.CardPriceSetting()
            {
                Area = this.SiteCode,
                CardCode = setting.CardCode,
                Num = 100,
                Price = decimal.Parse(txtPrice100)
            });

            return true;
        }

        private bool CheckTextBoxEmpty(RepeaterItem item, string itemName, string strMessage, bool mustNumber, out string strVal)
        {
            TextBox txt = item.FindControl(itemName) as TextBox;
            strVal = string.Empty;
            if (txt == null || string.IsNullOrEmpty(txt.Text) || (mustNumber && !Card.Utility.ValidationHelper.IsNumeric(txt.Text)))
            {
                if (!string.IsNullOrEmpty(strMessage))
                {
                    this.MessageBox(strMessage);
                }
                return false;
            }
            strVal = txt.Text;
            return true;
        }

        protected void lbtnShowDefault_Click(object sender, EventArgs e)
        {
            if (BLL.CardSettingManager.CopyCardSettingToSite("A", this.SiteCode, this.CPassport))
            {
                this.DoInitCardSetting();
                this.MessageBox("导入成功");
            }
        }
    }
}
